/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unisc.pdaapp.ui;

import br.unisc.pdaapp.constants.IPanels;
import br.unisc.pdaapp.meta.IForm;
import br.unisc.pdaapp.meta.ILoadableForm;
import br.unisc.pdaapp.meta.Student;
import br.unisc.pdaapp.meta.User;
import br.unisc.pdaapp.ui.messages.MessagesPanel;
import br.unisc.pdaapp.ui.messages.MessagesReaderPanel;
import br.unisc.pdaapp.ui.messages.MessagesSenderPanel;
import br.unisc.pdaapp.ui.messages.UserSelectionPanel;
import java.awt.CardLayout;
import java.awt.Component;
import java.util.Hashtable;

/**
 * @author guilherme


 */
public class MoodleMobileFrame extends javax.swing.JFrame implements IPanels
{

//    static
//    {
//        try
//        {
//            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getCrossPlatformLookAndFeelClassName());
//        }
//        catch (Exception exception)
//        {
//            System.out.println("Error loading L&F: " + exception);
//        }
//    }

    private User user = null;

    private CardLayout cardLayout;

    private static MoodleMobileFrame instance;

    private Hashtable forms;

    /** Creates new form Main */
    private MoodleMobileFrame()
    {
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:handlingClose
        //NSIcom won't close app without this
        this.addWindowListener(new java.awt.event.WindowAdapter() {
	    public void windowClosing(java.awt.event.WindowEvent e) {System.exit(0);}
	});
        // </editor-fold>//GEN-END:handlingClose
        initComponents();

        forms = new Hashtable();

        cardLayout = new CardLayout();
        contentPanel.setLayout(cardLayout);

        buildApp();
    }

    public static MoodleMobileFrame getInstance()
    {
        if (instance == null)
        {
            instance = new MoodleMobileFrame();
        }

        return instance;
    }

    public void quit(Component parent)
    {
//        int confirm = JOptionPane.showConfirmDialog(
//                parent, "Sair?",
//                "Moodle Mobile",
//                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
//
//        if (confirm == JOptionPane.YES_OPTION)
        {
            dispose();
            System.exit(0);
        }
    }

    private void buildApp()
    {
        forms.put(LOGIN_PANE, LoginPanel.getInstance());
        forms.put(ABOUT_PANE, AboutPanel.getInstance());
        forms.put(MESSAGES_PANE, MessagesPanel.getInstance());
        forms.put(MESSAGES_SENDER_PANE, MessagesSenderPanel.getInstance());
        forms.put(MESSAGES_READER_PANE, MessagesReaderPanel.getInstance());
        forms.put(COURSES_PANE, CoursesPanel.getInstance());
        forms.put(CLASSES_PANE, CourseClasseslPanel.getInstance());
        forms.put(MATERIAL_PANE, MaterialPanel.getInstance());
        forms.put(SELECT_TARGETS_PANE, UserSelectionPanel.getInstance());
        forms.put(UPLOAD_PANE, UploadPanel.getInstance());

        contentPanel.add(LOGIN_PANE, (Component) forms.get(LOGIN_PANE));
        contentPanel.add(ABOUT_PANE, (Component) forms.get(ABOUT_PANE));
        contentPanel.add(MESSAGES_PANE, (Component) forms.get(MESSAGES_PANE));
        contentPanel.add(MESSAGES_SENDER_PANE, (Component) forms.get(MESSAGES_SENDER_PANE));
        contentPanel.add(MESSAGES_READER_PANE, (Component) forms.get(MESSAGES_READER_PANE));
        contentPanel.add(COURSES_PANE, (Component) forms.get(COURSES_PANE));
        contentPanel.add(CLASSES_PANE, (Component) forms.get(CLASSES_PANE));
        contentPanel.add(MATERIAL_PANE, (Component) forms.get(MATERIAL_PANE));
        contentPanel.add(SELECT_TARGETS_PANE, (Component) forms.get(SELECT_TARGETS_PANE));
        contentPanel.add(UPLOAD_PANE, (Component) forms.get(UPLOAD_PANE));
    }

    public void switchPanel(String panelName)
    {
        if (forms.get(panelName) instanceof IForm)
        {
            IForm form = (IForm) forms.get(panelName);
            form.setHeader("Logado: " + user.getUserName());
        }

        if (forms.get(panelName) instanceof ILoadableForm)
        {
            ILoadableForm form = (ILoadableForm) forms.get(panelName);
            form.loadForm();
        }

        cardLayout.show(contentPanel, panelName);
    }

    public boolean isStudent()
    {
        return user instanceof Student;
    }

    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        contentPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        contentPanel.setBackground(new java.awt.Color(255, 255, 255));
        contentPanel.setLayout(new java.awt.BorderLayout());
        getContentPane().add(contentPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel contentPanel;
    // End of variables declaration//GEN-END:variables
}
